import pymysql

class pymysql_utils:
    def __init__(self,host, port ,user, password,database):
        self.host = host
        self.port = port
        self.user = user
        self.password=password
        self.database=database
        
    def open(self):
        self.conn = pymysql.connect(
            host = self.host,
            user = self.user,
            password = self.password,
            database = self.database,
            port = self.port,
            charset="utf8"
        )
        self.cursor = self.conn.cursor()

    def close(self):
        self.cursor.close()
        self.conn.close()

    def insert(self,sql, params):
        try:
            self.open()
            # 执行sql语句
            self.cursor.execute(sql, params)
            # 提交事务
            self.conn.commit()
            self.close()
        except Exception as e:
            print(e)
        